package com.kc.gb.device.starter;

import com.kc.gb.device.common.adapter.DeviceAdaptor;
import com.kc.gb.device.common.adapter.StationAdaptor;
import com.kc.gb.device.common.properties.DeviceProperties;
import com.kc.gb.device.common.utils.SpringHelper;
import com.kc.gb.device.service.DeviceMQService;
import com.kc.gb.device.service.WallPtlAdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : GlobalApplicationRunner
 * @version V1.0
 * @date: 2023/11/29 10:55 AM
 * @Description: TODO
 */
@Slf4j
@Component
public class DeviceApplicationRunner implements ApplicationRunner {

    @Resource
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Resource
    DeviceAdaptor deviceAdaptor;

    @Resource
    DeviceProperties deviceProperties;

    @Resource
    DeviceMQService iDeviceMQService;

    @Resource
    WallPtlAdminService wallPtlAdminService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 缓存预热
        threadPoolTaskExecutor.execute(()->{
            try {
                long warehouseId = deviceProperties.getWarehouseId();
                String stationCode = deviceAdaptor.getApplicationStationCode();
                StationAdaptor adaptor = SpringHelper.getApplicationContext().getBean(StationAdaptor.class);

                if(StringUtils.isNotEmpty(stationCode)){
                    wallPtlAdminService.getBasicStationDetail(warehouseId,stationCode);
                }
            } catch (Exception e) {
                log.error("PTLHardware:{}",e.getMessage(),e);
            }
        });

        // MQ 客户端创建
        threadPoolTaskExecutor.execute(()->{
            try {
                iDeviceMQService.createCommandProducer();
            } catch (Exception e) {
                log.error("PTLHardware:{}",e.getMessage(),e);
            }
        });
    }
}
